using System;
using System.Web.Mvc;
using Coleotrack.Settings;

namespace Coleotrack
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class IfInstalled : FilterAttribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!MvcApplication.ApplicationSettingsLoader.Settings.IsInstalled)
            {
                var urlHelper = new UrlHelper(filterContext.HttpContext.Request.RequestContext);
                filterContext.HttpContext.Response.Redirect(urlHelper.Action("Index", "Setup"));
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }
    }

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class IfNotInstalled : FilterAttribute, IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (MvcApplication.ApplicationSettingsLoader.Settings.IsInstalled)
            {
                var urlHelper = new UrlHelper(filterContext.HttpContext.Request.RequestContext);
                filterContext.HttpContext.Response.Redirect(urlHelper.Action("Logon", "Account"));
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }
    }
}